import axios from 'axios'
import {
  getTicket
} from '@/utils/auth'

// 创建axios实例
const $axios = axios.create({
  baseURL: process.env.GATEWAY, // api的base_url
  timeout: 5 * 60 * 1000 // 请求超时时间
})

// request拦截器
$axios.interceptors.request.use(
  config => {
    var ticket = getTicket()
    if (ticket) {
      config.headers['Authorization'] = `acb ${ticket}`
    } else {
      config.headers['Authorization'] = ' basic config'
    }
    return config
  },
  error => {
    return Promise.reject({
      status: -1,
      message: error.message
    })
  }
)

// response拦截器
$axios.interceptors.response.use(
  response => {
    var data = response.data
    if (data.hasOwnProperty('ok') && !data.ok) {
      return Promise.reject({
        status: -1,
        message: data.message
      })
    }
    return data
  },
  error => {
    return Promise.reject({
      status: -1,
      message: error.message
    })
  }
)
export default $axios
